//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//  CardView.m
//  PokerHandUp
//
//  Created by psvn047 on 6/12/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#import "CardView.h"
#import <QuartzCore/QuartzCore.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@implementation CardView
@synthesize card = _card;
@synthesize typeCard = _typeCard;
#define DURATION  0.5f
bool isWinAnimation = false;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    
        // add highlight
        /*UIView* rectView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 42, 63)] autorelease];
        [rectView drawRect:CGRectMake(0, 0, 42, 63)];
        [self addSubview:rectView];*/
    }
    return self;
}
-(void) stopAnimation
{
    isWinAnimation = false;
}
-(void) initData:(Card*) cValue typeCard:(NSString*) tCard
{
    self.card = cValue;
    self.typeCard = tCard;
}

-(void)dealloc
{
    [super dealloc];
    [self.card release];
    self.card = nil;
    [self.typeCard release];
    self.typeCard = nil;
}
// flip card animation
-(void) playAnimation
{
    self.highlighted = YES;
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:@"playAnimation" context:context];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self cache:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.3];
    [UIView commitAnimations];
}
// vibrate card animation
-(void) winAnimation
{
    isWinAnimation = true;
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:@"zoomInAnimation" context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:DURATION];
    [self setFrame:CGRectMake(self.frame.origin.x - 2, self.frame.origin.y -2 , self.frame.size.width + 4, self.frame.size.height + 4)];
    [UIView commitAnimations];

}
-(void) animationDidStop:(NSString*) animationID finished:(NSNumber*) finished context:(void*) context
{
    if(!isWinAnimation)
    {
        return;
    }
    
    if (animationID == @"zoomInAnimation") {
        CGContextRef context = UIGraphicsGetCurrentContext();
        [UIView beginAnimations:@"zoomOutAnimation" context:context];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:DURATION];
        [self setFrame:CGRectMake(self.frame.origin.x + 2, self.frame.origin.y + 2 , self.frame.size.width - 4, self.frame.size.height - 4)];
        [UIView commitAnimations];
    }
    else if (animationID == @"zoomOutAnimation") {
        CGContextRef context = UIGraphicsGetCurrentContext();
        [UIView beginAnimations:@"zoomInAnimation" context:context];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:DURATION];
        [self setFrame:CGRectMake(self.frame.origin.x - 2, self.frame.origin.y - 2 , self.frame.size.width + 4, self.frame.size.height + 4)];
        [UIView commitAnimations];

    }
}
@end
////////////////////////////////////////////////////////////////////////////////////////////////////////////////